home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / Managed / Common / dxmutSettingsDlg.cs < prev   
Encoding:
Text File  |  2004-09-27  |  38.5 KB  |  835 lines

  1. //--------------------------------------------------------------------------------------
  2. // File: DXMUTSettingsDlg.cs
  3. //
  4. // Dialog for selection of device settings 
  5. //
  6. // Copyright (c) Microsoft Corporation. All rights reserved.
  7. //--------------------------------------------------------------------------------------
  8. using System;
  9. using System.IO;
  10. using System.Collections;
  11. using System.Runtime.InteropServices;
  12. using Microsoft.DirectX;
  13. using Microsoft.DirectX.Direct3D;
  14.  
  15. namespace Microsoft.Samples.DirectX.UtilityToolkit
  16. {
  17.     #region Control Ids
  18.     public enum SettingsDialogControlIds
  19.     {
  20.         Static = -1,
  21.         None,
  22.         OK,
  23.         Cancel,
  24.         Adapter,
  25.         DeviceType,
  26.         Windowed,
  27.         Fullscreen,
  28.         AdapterFormat,
  29.         AdapterFormatLabel,
  30.         Resolution,
  31.         ResolutionLabel,
  32.         RefreshRate,
  33.         RefreshRateLabel,
  34.         BackBufferFormat,
  35.         DepthStencil,
  36.         MultisampleType,
  37.         MultisampleQuality,
  38.         VertexProcessing,
  39.         PresentInterval,
  40.         DeviceClip,
  41.         RadioButtonGroup = 0x100,
  42.     }
  43.     #endregion
  44.  
  45.     /// <summary>
  46.     /// Dialog for selection of device settings 
  47.     /// </summary>
  48.     public class SettingsDialog
  49.     {
  50.         #region Creation
  51.         /// <summary>Creates a new settings dialog</summary>
  52.         public SettingsDialog(Framework sample) 
  53.         {
  54.             parent = sample;
  55.             windowWidth = Framework.DefaultSizeWidth; windowHeight = Framework.DefaultSizeHeight;
  56.             CreateControls();
  57.         } 
  58.         #endregion
  59.  
  60.         #region Class Data
  61.         private Framework parent; // Parent framework for this dialog
  62.         private Dialog dialog; // Dialog that will be rendered
  63.         private uint windowWidth; // Width of window
  64.         private uint windowHeight; // Height of window
  65.         private DeviceSettings globalSettings; // Device settings
  66.         #endregion
  67.  
  68.         #region Control variables
  69.         // Combo boxes
  70.         private ComboBox resolution;
  71.         private ComboBox adapterCombo;
  72.         private ComboBox deviceCombo;
  73.         private ComboBox adapterFormatCombo;
  74.         private ComboBox refreshCombo;
  75.         private ComboBox backBufferCombo;
  76.         private ComboBox depthStencilCombo;
  77.         private ComboBox multiSampleTypeCombo;
  78.         private ComboBox multiSampleQualityCombo;
  79.         private ComboBox vertexCombo;
  80.         private ComboBox presentCombo;
  81.         // Check boxes
  82.         private Checkbox clipBox;
  83.         // Radio buttons
  84.         private RadioButton windowedButton;
  85.         private RadioButton fullscreenButton;
  86.         // Static controls that are cared about
  87.         private StaticText adapterFormatStatic;
  88.         private StaticText resolutionStatic;
  89.         private StaticText refreshStatic;
  90.         #endregion
  91.  
  92.         /// <summary>
  93.         /// Creates the controls for use in the dialog
  94.         /// </summary>
  95.         private void CreateControls()
  96.         {
  97.             dialog = new Dialog(parent);
  98.             dialog.IsUsingKeyboardInput = true;
  99.             dialog.SetFont(0, "Arial", 15, FontWeight.Normal);
  100.             dialog.SetFont(1, "Arial", 28, FontWeight.Bold);
  101.             
  102.             // Right justify static controls
  103.             Element e = dialog.GetDefaultElement(ControlType.StaticText, 0);
  104.             e.textFormat = DrawTextFormat.VerticalCenter | DrawTextFormat.Right;
  105.  
  106.             // Title
  107.             StaticText title = dialog.AddStatic((int)SettingsDialogControlIds.Static, "Direct3D Settings", 10, 5, 400, 50);
  108.             e = title[0];
  109.             e.FontIndex = 1;
  110.             e.textFormat = DrawTextFormat.Top | DrawTextFormat.Left;
  111.  
  112.             // Adapter
  113.             dialog.AddStatic((int)SettingsDialogControlIds.Static, "Display Adapter", 10, 50, 180, 23);
  114.             adapterCombo = dialog.AddComboBox((int)SettingsDialogControlIds.Adapter, 200, 50, 300, 23);
  115.  
  116.             // Device Type
  117.             dialog.AddStatic((int)SettingsDialogControlIds.Static, "Render Device", 10, 75, 180, 23);
  118.             deviceCombo = dialog.AddComboBox((int)SettingsDialogControlIds.DeviceType, 200, 75, 300, 23);
  119.  
  120.             // Windowed / Fullscreen
  121.             windowedButton = dialog.AddRadioButton((int)SettingsDialogControlIds.Windowed, (int)SettingsDialogControlIds.RadioButtonGroup, 
  122.                 "Windowed", 240, 105, 300, 16, false);
  123.             clipBox = dialog.AddCheckBox((int)SettingsDialogControlIds.DeviceClip, "Clip to device when window spans across multiple monitors", 
  124.                 250, 126, 400, 16, false);
  125.             fullscreenButton = dialog.AddRadioButton((int)SettingsDialogControlIds.Fullscreen, (int)SettingsDialogControlIds.RadioButtonGroup, "Full Screen", 
  126.                 240, 147, 300, 16, false);
  127.  
  128.             // Adapter Format
  129.             adapterFormatStatic = dialog.AddStatic((int)SettingsDialogControlIds.AdapterFormatLabel, "Adapter Format", 
  130.                 10, 180, 180, 23);
  131.             adapterFormatCombo = dialog.AddComboBox((int)SettingsDialogControlIds.AdapterFormat, 200, 180, 300, 23);
  132.  
  133.             // Resolution
  134.             resolutionStatic = dialog.AddStatic((int)SettingsDialogControlIds.ResolutionLabel, "Resolution", 10, 205, 180, 23);
  135.             resolution = dialog.AddComboBox((int)SettingsDialogControlIds.Resolution, 200, 205, 300, 23);
  136.             resolution.SetDropHeight(106);
  137.  
  138.             // Refresh Rate
  139.             refreshStatic = dialog.AddStatic((int)SettingsDialogControlIds.RefreshRateLabel, "Refresh Rate", 10, 230, 180, 23);
  140.             refreshCombo = dialog.AddComboBox((int)SettingsDialogControlIds.RefreshRate, 200, 230, 300, 23);
  141.  
  142.             // BackBuffer Format
  143.             dialog.AddStatic((int)SettingsDialogControlIds.Static, "Back Buffer Format", 10, 265, 180, 23 );
  144.             backBufferCombo = dialog.AddComboBox((int)SettingsDialogControlIds.BackBufferFormat, 200, 265, 300, 23 );
  145.  
  146.             // Depth Stencil
  147.             dialog.AddStatic((int)SettingsDialogControlIds.Static, "Depth/Stencil Format", 10, 290, 180, 23 );
  148.             depthStencilCombo = dialog.AddComboBox((int)SettingsDialogControlIds.DepthStencil, 200, 290, 300, 23 );
  149.  
  150.             // Multisample Type
  151.             dialog.AddStatic((int)SettingsDialogControlIds.Static, "Multisample Type", 10, 315, 180, 23 );
  152.             multiSampleTypeCombo = dialog.AddComboBox((int)SettingsDialogControlIds.MultisampleType, 200, 315, 300, 23 );
  153.  
  154.             // Multisample Quality
  155.             dialog.AddStatic((int)SettingsDialogControlIds.Static, "Multisample Quality", 10, 340, 180, 23 );
  156.             multiSampleQualityCombo = dialog.AddComboBox((int)SettingsDialogControlIds.MultisampleQuality, 200, 340, 300, 23 );
  157.  
  158.             // Vertex Processing
  159.             dialog.AddStatic((int)SettingsDialogControlIds.Static, "Vertex Processing", 10, 365, 180, 23 );
  160.             vertexCombo = dialog.AddComboBox((int)SettingsDialogControlIds.VertexProcessing, 200, 365, 300, 23 );
  161.  
  162.             // Present Interval
  163.             dialog.AddStatic((int)SettingsDialogControlIds.Static, "Present Interval", 10, 390, 180, 23 );
  164.             presentCombo = dialog.AddComboBox((int)SettingsDialogControlIds.PresentInterval, 200, 390, 300, 23 );
  165.  
  166.             // Add the ok/cancel buttons
  167.             Button okButton = dialog.AddButton((int)SettingsDialogControlIds.OK, "OK", 230,435,73,31);
  168.             Button cancelButton = dialog.AddButton((int)SettingsDialogControlIds.Cancel, "Cancel", 315,435,73,31,0, true);
  169.             okButton.Click += new EventHandler(OnOkClicked);
  170.             cancelButton.Click += new EventHandler(OnCancelClicked);
  171.         }
  172.  
  173.         /// <summary>Changes the UI defaults to the current device settings</summary>
  174.         public void Refresh()
  175.         {
  176.             // Get some information
  177.             globalSettings = parent.DeviceSettings.Clone();
  178.             System.Drawing.Rectangle client = parent.WindowClientRectangle;
  179.             windowWidth = (uint)client.Width;
  180.             windowHeight = (uint)client.Height;
  181.  
  182.             // Fill the UI with the current settings
  183.             if (!deviceCombo.ContainsItem(globalSettings.DeviceType.ToString()))
  184.                 deviceCombo.AddItem(globalSettings.DeviceType.ToString(), globalSettings.DeviceType.ToString());
  185.  
  186.             SetWindowed(globalSettings.presentParams.Windowed);
  187.             clipBox.IsChecked = ((globalSettings.presentParams.PresentFlag & PresentFlag.DeviceClip) != 0);
  188.  
  189.             if (!adapterFormatCombo.ContainsItem(globalSettings.AdapterFormat.ToString()))
  190.                 adapterFormatCombo.AddItem(globalSettings.AdapterFormat.ToString(), globalSettings.AdapterFormat);
  191.  
  192.             AddResolution((short)globalSettings.presentParams.BackBufferWidth, (short)globalSettings.presentParams.BackBufferHeight);
  193.             AddRefreshRate(globalSettings.presentParams.FullScreenRefreshRateInHz);
  194.  
  195.             if (!backBufferCombo.ContainsItem(globalSettings.presentParams.BackBufferFormat.ToString()))
  196.                 backBufferCombo.AddItem(globalSettings.presentParams.BackBufferFormat.ToString(), globalSettings.presentParams.BackBufferFormat);
  197.  
  198.             if (!depthStencilCombo.ContainsItem(globalSettings.presentParams.AutoDepthStencilFormat.ToString()))
  199.                 depthStencilCombo.AddItem(globalSettings.presentParams.AutoDepthStencilFormat.ToString(), globalSettings.presentParams.AutoDepthStencilFormat);
  200.  
  201.             if (!multiSampleTypeCombo.ContainsItem(globalSettings.presentParams.MultiSample.ToString()))
  202.                 multiSampleTypeCombo.AddItem(globalSettings.presentParams.MultiSample.ToString(), globalSettings.presentParams.MultiSample);
  203.  
  204.             if (!multiSampleQualityCombo.ContainsItem(globalSettings.presentParams.MultiSampleQuality.ToString()))
  205.                 multiSampleQualityCombo.AddItem(globalSettings.presentParams.MultiSampleQuality.ToString(), globalSettings.presentParams.MultiSampleQuality);
  206.  
  207.             if (!presentCombo.ContainsItem(globalSettings.presentParams.PresentationInterval.ToString()))
  208.                 presentCombo.AddItem(globalSettings.presentParams.PresentationInterval.ToString(), globalSettings.presentParams.PresentationInterval);
  209.  
  210.             BehaviorFlags flags = new BehaviorFlags(globalSettings.BehaviorFlags);
  211.             if (flags.PureDevice)
  212.                 AddVertexProcessing(CreateFlags.PureDevice);
  213.             else if (flags.HardwareVertexProcessing)
  214.                 AddVertexProcessing(CreateFlags.HardwareVertexProcessing);
  215.             else if (flags.SoftwareVertexProcessing)
  216.                 AddVertexProcessing(CreateFlags.SoftwareVertexProcessing);
  217.             else if (flags.MixedVertexProcessing)
  218.                 AddVertexProcessing(CreateFlags.MixedVertexProcessing);
  219.  
  220.             // Get the adapters list from Enumeration object
  221.             ArrayList adapterInfoList = Enumeration.AdapterInformationList;
  222.  
  223.             if (adapterInfoList.Count == 0)
  224.                 throw new NoCompatibleDevicesException();
  225.  
  226.             adapterCombo.Clear();
  227.             
  228.             // Add all of the adapters
  229.             for (int iAdapter = 0; iAdapter < adapterInfoList.Count; iAdapter++)
  230.             {
  231.                 EnumAdapterInformation adapterInfo = adapterInfoList[iAdapter] as EnumAdapterInformation;
  232.                 if (!adapterCombo.ContainsItem(adapterInfo.UniqueDescription))
  233.                     adapterCombo.AddItem(adapterInfo.UniqueDescription, iAdapter);
  234.             }
  235.             adapterCombo.SetSelectedByData(globalSettings.AdapterOrdinal);
  236.  
  237.             // The adapter changed, call the handler
  238.             OnAdapterChanged(adapterCombo, EventArgs.Empty);
  239.  
  240.             Dialog.SetRefreshTime((float)FrameworkTimer.GetTime());
  241.         }
  242.  
  243.         /// <summary>Render the dialog</summary>
  244.         public void OnRender(float elapsedTime)
  245.         {
  246.             dialog.OnRender(elapsedTime);
  247.         }
  248.  
  249.         /// <summary>Hand messages off to dialog</summary>
  250.         public void HandleMessages(IntPtr hWnd, NativeMethods.WindowMessage msg, IntPtr wParam, IntPtr lParam)
  251.         {
  252.             dialog.MessageProc(hWnd, msg, wParam, lParam);
  253.         }
  254.         #region Device event callbacks
  255.         /// <summary>
  256.         /// Called when the device is created
  257.         /// </summary>
  258.         public void OnCreateDevice(Device d)  
  259.         { 
  260.             // Hook all the events we care about
  261.             resolution.Changed += new EventHandler(OnResolutionChanged);
  262.             adapterCombo.Changed += new EventHandler(OnAdapterChanged);
  263.             deviceCombo.Changed += new EventHandler(OnDeviceChanged);
  264.             adapterFormatCombo.Changed += new EventHandler(OnAdapterFormatChange);
  265.             refreshCombo.Changed += new EventHandler(OnRefreshRateChanged);
  266.             backBufferCombo.Changed += new EventHandler(OnBackBufferChanged);
  267.             depthStencilCombo.Changed += new EventHandler(OnDepthStencilChanged);
  268.             multiSampleTypeCombo.Changed += new EventHandler(OnMultisampleTypeChanged);
  269.             multiSampleQualityCombo.Changed += new EventHandler(OnMultisampleQualityChanged);
  270.             vertexCombo.Changed += new EventHandler(OnVertexProcessingChanged);
  271.             presentCombo.Changed += new EventHandler(OnPresentIntervalChanged);
  272.             clipBox.Changed += new EventHandler(OnClipWindowChanged);
  273.             windowedButton.Changed += new EventHandler(OnWindowedFullscreenChanged);
  274.             fullscreenButton.Changed += new EventHandler(OnWindowedFullscreenChanged);
  275.         } 
  276.  
  277.         /// <summary>
  278.         /// Called when the device is reset
  279.         /// </summary>
  280.         public void OnResetDevice()
  281.         {
  282.             SurfaceDescription desc = parent.BackBufferSurfaceDescription;
  283.  
  284.             // Set up the dialog
  285.             dialog.SetLocation(0, 0);
  286.             dialog.SetSize(desc.Width, desc.Height);
  287.             dialog.SetBackgroundColors(new ColorValue((float)98/255, (float)138/255, (float)206/255),
  288.                 new ColorValue((float)54/255, (float)105/255, (float)192/255),
  289.                 new ColorValue((float)54/255, (float)105/255, (float)192/255),
  290.                 new ColorValue((float)10/255, (float)73/255, (float)179/255) );
  291.  
  292.         }
  293.  
  294.         /// <summary>Destroy any resources</summary>
  295.         private void OnDestroyDevice(object sender, EventArgs e)
  296.         {
  297.             // Clear the focus
  298.             Dialog.ClearFocus();
  299.             // Unhook all the events we care about
  300.             resolution.Changed -= new EventHandler(OnResolutionChanged);
  301.             adapterCombo.Changed -= new EventHandler(OnAdapterChanged);
  302.             deviceCombo.Changed -= new EventHandler(OnDeviceChanged);
  303.             adapterFormatCombo.Changed -= new EventHandler(OnAdapterFormatChange);
  304.             refreshCombo.Changed -= new EventHandler(OnRefreshRateChanged);
  305.             backBufferCombo.Changed -= new EventHandler(OnBackBufferChanged);
  306.             depthStencilCombo.Changed -= new EventHandler(OnDepthStencilChanged);
  307.             multiSampleTypeCombo.Changed -= new EventHandler(OnMultisampleTypeChanged);
  308.             multiSampleQualityCombo.Changed -= new EventHandler(OnMultisampleQualityChanged);
  309.             vertexCombo.Changed -= new EventHandler(OnVertexProcessingChanged);
  310.             presentCombo.Changed -= new EventHandler(OnPresentIntervalChanged);
  311.             clipBox.Changed -= new EventHandler(OnClipWindowChanged);
  312.             windowedButton.Changed -= new EventHandler(OnWindowedFullscreenChanged);
  313.             fullscreenButton.Changed -= new EventHandler(OnWindowedFullscreenChanged);
  314.         }
  315.  
  316.         #endregion
  317.  
  318.         /// <summary>Returns the current device information</summary>
  319.         private EnumDeviceInformation GetCurrentDeviceInfo()
  320.         {
  321.             return Enumeration.GetDeviceInfo(globalSettings.AdapterOrdinal, globalSettings.DeviceType);
  322.         }
  323.         /// <summary>Returns the current adapter information</summary>
  324.         private EnumAdapterInformation GetCurrentAdapterInfo()
  325.         {
  326.             return Enumeration.GetAdapterInformation(globalSettings.AdapterOrdinal);
  327.         }
  328.         /// <summary>Returns the current adapter information</summary>
  329.         private EnumDeviceSettingsCombo GetCurrentDeviceSettingsCombo()
  330.         {
  331.             return Enumeration.GetDeviceSettingsCombo(globalSettings.AdapterOrdinal,
  332.                 globalSettings.DeviceType, globalSettings.AdapterFormat, 
  333.                 globalSettings.presentParams.BackBufferFormat, globalSettings.presentParams.Windowed);
  334.         }
  335.  
  336.         // TODO: SetDeviceSettingsFromUI
  337.         #region Update UI Methods
  338.         /// <summary>Sets whether this is running in windowed or fullscreen mode</summary>
  339.         private void SetWindowed(bool windowed)
  340.         {
  341.             windowedButton.IsChecked = windowed;
  342.             fullscreenButton.IsChecked = !windowed;
  343.         }
  344.         /// <summary>Adds a resolution to the combo box</summary>
  345.         private void AddResolution(short width, short height)
  346.         {
  347.             string itemText = string.Format("{0} by {1}", width, height);
  348.             // Store the resolution in a single variable
  349.             uint resolutionData = NativeMethods.MakeUInt32(width, height);
  350.  
  351.             // Add this item
  352.             if (!resolution.ContainsItem(itemText))
  353.                 resolution.AddItem(itemText, resolutionData);
  354.         }
  355.         /// <summary>Adds a refresh rate to the combo box</summary>
  356.         private void AddRefreshRate(int rate)
  357.         {
  358.             string itemText = (rate == 0) ? "Default Rate" : string.Format("{0} Hz", rate);
  359.             // Add this item
  360.             if (!refreshCombo.ContainsItem(itemText))
  361.                 refreshCombo.AddItem(itemText, rate);
  362.         }
  363.         /// <summary>Adds a vertex processing type to the combo box</summary>
  364.         private void AddVertexProcessing(CreateFlags flags)
  365.         {
  366.             string itemText = "Unknown vertex processing type";
  367.             switch(flags)
  368.             {
  369.                 case CreateFlags.PureDevice:
  370.                     itemText = "Pure hardware vertex processing"; break;
  371.                 case CreateFlags.HardwareVertexProcessing:
  372.                     itemText = "Hardware vertex processing"; break;
  373.                 case CreateFlags.SoftwareVertexProcessing:
  374.                     itemText = "Software vertex processing"; break;
  375.                 case CreateFlags.MixedVertexProcessing:
  376.                     itemText = "Mixed vertex processing"; break;
  377.             }
  378.             // Add this item
  379.             if (!vertexCombo.ContainsItem(itemText))
  380.                 vertexCombo.AddItem(itemText, flags);
  381.         }
  382.         #endregion
  383.  
  384.         #region Event handlers for the controls
  385.         /// <summary>Called when the resolution changes</summary>
  386.         private void OnResolutionChanged(object sender, EventArgs e)
  387.         {
  388.             ComboBox cb = sender as ComboBox;
  389.  
  390.             // Set the resolution
  391.             uint data = (uint)cb.GetSelectedData();
  392.             int width = NativeMethods.LoWord(data);
  393.             int height = NativeMethods.HiWord(data);
  394.             globalSettings.presentParams.BackBufferWidth = width;
  395.             globalSettings.presentParams.BackBufferHeight = height;
  396.  
  397.             int refreshRate = globalSettings.presentParams.FullScreenRefreshRateInHz;
  398.  
  399.             // Update the refresh rate list
  400.             refreshCombo.Clear();
  401.  
  402.             EnumAdapterInformation adapterInfo = GetCurrentAdapterInfo();
  403.             Format adapterFormat = globalSettings.AdapterFormat;
  404.             foreach(DisplayMode dm in adapterInfo.displayModeList)
  405.             {
  406.                 if (dm.Format == adapterFormat &&
  407.                     dm.Width == width &&
  408.                     dm.Height == height)
  409.                 {
  410.                     AddRefreshRate(dm.RefreshRate);
  411.                 }
  412.             }
  413.  
  414.             // select and update
  415.             refreshCombo.SetSelectedByData(refreshRate);
  416.             OnRefreshRateChanged(refreshCombo, e);
  417.         }
  418.  
  419.         /// <summary>Called when the adapter changes</summary>
  420.         private void OnAdapterChanged(object sender, EventArgs e)
  421.         {
  422.             ComboBox cb = sender as ComboBox;
  423.             // Store the adapter index
  424.             globalSettings.AdapterOrdinal = (uint)(int)cb.GetSelectedData();
  425.  
  426.             // Remove all the items from the device type
  427.             deviceCombo.Clear();
  428.  
  429.             // Get the adapter information
  430.             EnumAdapterInformation adapterInfo = GetCurrentAdapterInfo();
  431.  
  432.             // Add each device type to the combo list
  433.             foreach(EnumDeviceInformation edi in adapterInfo.deviceInfoList)
  434.             {
  435.                 if (!deviceCombo.ContainsItem(edi.DeviceType.ToString()))
  436.                     deviceCombo.AddItem(edi.DeviceType.ToString(), edi.DeviceType);
  437.             }
  438.             deviceCombo.SetSelectedByData(globalSettings.DeviceType);
  439.  
  440.             // Device type was changed update
  441.             OnDeviceChanged(deviceCombo, e);
  442.         }
  443.         /// <summary>Called when the device type changes</summary>
  444.         private void OnDeviceChanged(object sender, EventArgs e)
  445.         {
  446.             ComboBox cb = sender as ComboBox;
  447.             globalSettings.DeviceType = (DeviceType)cb.GetSelectedData();
  448.  
  449.             // Update windowed/full screen radio buttons
  450.             bool hasWindowCombo = false;
  451.             bool hasFullscreen = false;
  452.  
  453.             EnumDeviceInformation edi = GetCurrentDeviceInfo();
  454.  
  455.             // See if there are any windowed/fullscreen combos
  456.             foreach(EnumDeviceSettingsCombo edsc in edi.deviceSettingsList)
  457.             {
  458.                 if (edsc.IsWindowed)
  459.                     hasWindowCombo = true;
  460.                 else
  461.                     hasFullscreen = true;
  462.             }
  463.  
  464.             // Set the controls enable/disable property based on whether they are available or not
  465.             dialog.SetControlEnable((int)SettingsDialogControlIds.Windowed, hasWindowCombo);
  466.             dialog.SetControlEnable((int)SettingsDialogControlIds.Fullscreen, hasFullscreen);
  467.  
  468.             SetWindowed(globalSettings.presentParams.Windowed && hasWindowCombo);
  469.  
  470.             OnWindowedFullscreenChanged(null, e);
  471.         }
  472.  
  473.         /// <summary>Called when the adapter format changes</summary>
  474.         private void OnAdapterFormatChange(object sender, EventArgs e)
  475.         {
  476.             ComboBox cb = sender as ComboBox;
  477.             Format adapterFormat = (Format)cb.GetSelectedData();
  478.  
  479.             // Resolutions
  480.             resolution.Clear();
  481.  
  482.             EnumAdapterInformation adapterInfo = GetCurrentAdapterInfo();
  483.             foreach(DisplayMode dm in adapterInfo.displayModeList)
  484.             {
  485.                 if (dm.Format == adapterFormat)
  486.                     AddResolution((short)dm.Width, (short)dm.Height);
  487.             }
  488.  
  489.             uint currentResolution = NativeMethods.MakeUInt32(
  490.                 (short)globalSettings.presentParams.BackBufferWidth, (short)globalSettings.presentParams.BackBufferHeight);
  491.  
  492.             resolution.SetSelectedByData(currentResolution);
  493.             // Resolution changed
  494.             OnResolutionChanged(resolution, e);
  495.  
  496.             // Back buffer formats
  497.             backBufferCombo.Clear();
  498.  
  499.             EnumDeviceInformation edi = GetCurrentDeviceInfo();
  500.             bool hasWindowedBackBuffer = false;
  501.             bool isWindowed = windowedButton.IsChecked;
  502.  
  503.             foreach(EnumDeviceSettingsCombo edsc in edi.deviceSettingsList)
  504.             {
  505.                 if (edsc.IsWindowed == isWindowed &&
  506.                     edsc.AdapterFormat == globalSettings.AdapterFormat)
  507.                 {
  508.                     hasWindowedBackBuffer = true;
  509.                     if (!backBufferCombo.ContainsItem(edsc.BackBufferFormat.ToString()))
  510.                         backBufferCombo.AddItem(edsc.BackBufferFormat.ToString(), edsc.BackBufferFormat);
  511.                 }
  512.             }
  513.             // Update back buffer
  514.             backBufferCombo.SetSelectedByData(globalSettings.presentParams.BackBufferFormat);
  515.             OnBackBufferChanged(backBufferCombo, e);
  516.  
  517.             if (!hasWindowedBackBuffer)
  518.             {
  519.                 dialog.SetControlEnable((int)SettingsDialogControlIds.Windowed, false);
  520.                 if (globalSettings.presentParams.Windowed)
  521.                 {
  522.                     SetWindowed(false);
  523.                     OnWindowedFullscreenChanged(null, e);
  524.                 }
  525.             }
  526.         }
  527.  
  528.         /// <summary>Called when the refresh rate changes</summary>
  529.         private void OnRefreshRateChanged(object sender, EventArgs e)
  530.         {
  531.             ComboBox c = sender as ComboBox;
  532.             globalSettings.presentParams.FullScreenRefreshRateInHz = (int)c.GetSelectedData();
  533.         }
  534.         
  535.         /// <summary>Called when the back buffer format changes</summary>
  536.         private void OnBackBufferChanged(object sender, EventArgs e)
  537.         {
  538.             ComboBox cb = sender as ComboBox;
  539.             globalSettings.presentParams.BackBufferFormat = (Format)backBufferCombo.GetSelectedData();
  540.  
  541.             // Store formats
  542.             Format adapterFormat = globalSettings.AdapterFormat;
  543.             Format backFormat = globalSettings.presentParams.BackBufferFormat;
  544.  
  545.             EnumDeviceInformation edi = GetCurrentDeviceInfo();
  546.  
  547.             // Get possible vertex processing
  548.             bool isAllowSoftware = Enumeration.IsSoftwareVertexProcessingPossible;
  549.             bool isAllowHardware = Enumeration.IsHardwareVertexProcessingPossible;
  550.             bool isAllowPure = Enumeration.IsPureHardwareVertexProcessingPossible;
  551.             bool isAllowMixed = Enumeration.IsMixedVertexProcessingPossible;
  552.  
  553.             foreach(EnumDeviceSettingsCombo edsc in edi.deviceSettingsList)
  554.             {
  555.                 if (edsc.IsWindowed == globalSettings.presentParams.Windowed &&
  556.                     edsc.AdapterFormat == adapterFormat &&
  557.                     edsc.BackBufferFormat == backFormat)
  558.                 {
  559.                     // Clear the depth stencil buffer
  560.                     depthStencilCombo.Clear();
  561.                     depthStencilCombo.IsEnabled = (globalSettings.presentParams.EnableAutoDepthStencil);
  562.                     if (globalSettings.presentParams.EnableAutoDepthStencil)
  563.                     {
  564.                         foreach(Format f in edsc.depthStencilFormatList)
  565.                         {
  566.                             if (!depthStencilCombo.ContainsItem(f.ToString()))
  567.                                 depthStencilCombo.AddItem(f.ToString(), f);
  568.                         }
  569.  
  570.                         depthStencilCombo.SetSelectedByData(globalSettings.presentParams.AutoDepthStencilFormat);
  571.                     }
  572.                     else
  573.                     {
  574.                         if (!depthStencilCombo.ContainsItem("(not used)") )
  575.                             depthStencilCombo.AddItem("(not used)", null);
  576.                     }
  577.                     OnDepthStencilChanged(depthStencilCombo, e);
  578.  
  579.                     // Now remove all the vertex processing information
  580.                     vertexCombo.Clear();
  581.                     if (isAllowPure)
  582.                         AddVertexProcessing(CreateFlags.PureDevice);
  583.                     if (isAllowHardware)
  584.                         AddVertexProcessing(CreateFlags.HardwareVertexProcessing);
  585.                     if (isAllowSoftware)
  586.                         AddVertexProcessing(CreateFlags.SoftwareVertexProcessing);
  587.                     if (isAllowMixed)
  588.                         AddVertexProcessing(CreateFlags.MixedVertexProcessing);
  589.  
  590.                     // Select the right one
  591.                     BehaviorFlags flags = new BehaviorFlags(globalSettings.BehaviorFlags);
  592.                     if (flags.PureDevice)
  593.                         vertexCombo.SetSelectedByData(CreateFlags.PureDevice);
  594.                     else if (flags.HardwareVertexProcessing)
  595.                         vertexCombo.SetSelectedByData(CreateFlags.HardwareVertexProcessing);
  596.                     else if (flags.SoftwareVertexProcessing)
  597.                         vertexCombo.SetSelectedByData(CreateFlags.SoftwareVertexProcessing);
  598.                     else if (flags.MixedVertexProcessing)
  599.                         vertexCombo.SetSelectedByData(CreateFlags.MixedVertexProcessing);
  600.  
  601.                     OnVertexProcessingChanged(vertexCombo, e);
  602.  
  603.                     // Now present intervals
  604.                     presentCombo.Clear();
  605.                     foreach(PresentInterval pf in edsc.presentIntervalList)
  606.                     {
  607.                         if (!presentCombo.ContainsItem(pf.ToString()))
  608.                             presentCombo.AddItem(pf.ToString(), pf);
  609.                     }
  610.  
  611.                     presentCombo.SetSelectedByData(globalSettings.presentParams.PresentationInterval);
  612.                     OnPresentIntervalChanged(presentCombo, e);
  613.                 }
  614.             }
  615.  
  616.         }
  617.         
  618.         /// <summary>Called when the depth stencil changes</summary>
  619.         private void OnDepthStencilChanged(object sender, EventArgs e)
  620.         {
  621.             ComboBox cb = sender as ComboBox;
  622.             DepthFormat stencilFormat = (DepthFormat)cb.GetSelectedData();
  623.  
  624.             if (globalSettings.presentParams.EnableAutoDepthStencil)
  625.                 globalSettings.presentParams.AutoDepthStencilFormat = stencilFormat;
  626.  
  627.             EnumDeviceSettingsCombo combo = GetCurrentDeviceSettingsCombo();
  628.  
  629.             // Remove all of the multisample items and add the new ones
  630.             multiSampleTypeCombo.Clear();
  631.             foreach(MultiSampleType mst in combo.multiSampleTypeList)
  632.             {
  633.                 bool conflictFound = false;
  634.                 foreach(EnumDepthStencilMultisampleConflict c in combo.depthStencilConflictList)
  635.                 {
  636.                     if (c.DepthStencilFormat == stencilFormat &&
  637.                         c.MultisampleType == mst)
  638.                     {
  639.                         conflictFound = true;
  640.                         break;
  641.                     }
  642.                 }
  643.  
  644.                 if (!conflictFound)
  645.                 {
  646.                     if (!multiSampleTypeCombo.ContainsItem(mst.ToString()))
  647.                         multiSampleTypeCombo.AddItem(mst.ToString(), mst);
  648.                 }
  649.             }
  650.             // Select the correct multisampling type
  651.             multiSampleTypeCombo.SetSelectedByData(globalSettings.presentParams.MultiSample);
  652.             OnMultisampleTypeChanged(multiSampleTypeCombo, e);
  653.         }
  654.         
  655.         /// <summary>Called when the multisample type changes</summary>
  656.         private void OnMultisampleTypeChanged(object sender, EventArgs e)
  657.         {
  658.             ComboBox cb = sender as ComboBox;
  659.             MultiSampleType mst = (MultiSampleType)cb.GetSelectedData();
  660.             globalSettings.presentParams.MultiSample = mst;
  661.  
  662.             EnumDeviceSettingsCombo combo = GetCurrentDeviceSettingsCombo();
  663.  
  664.             int maxQuality = 0;
  665.             for (int i = 0; i < combo.multiSampleTypeList.Count; i++)
  666.             {
  667.                 MultiSampleType msType = (MultiSampleType)combo.multiSampleTypeList[i];
  668.                 if (msType == mst)
  669.                 {
  670.                     maxQuality = (int)combo.multiSampleQualityList[i];
  671.                 }
  672.             }
  673.  
  674.             // We have the max quality now, add to our list
  675.             multiSampleQualityCombo.Clear();
  676.             for(int i = 0; i < maxQuality; i++)
  677.             {
  678.                 if (!multiSampleQualityCombo.ContainsItem(i.ToString()))
  679.                     multiSampleQualityCombo.AddItem(i.ToString(), i);
  680.             }
  681.             multiSampleQualityCombo.SetSelectedByData(globalSettings.presentParams.MultiSampleQuality);
  682.             OnMultisampleQualityChanged(multiSampleQualityCombo, e);
  683.         }
  684.         /// <summary>Called when the multisample quality changes</summary>
  685.         private void OnMultisampleQualityChanged(object sender, EventArgs e)
  686.         {
  687.             ComboBox cb = sender as ComboBox;
  688.             globalSettings.presentParams.MultiSampleQuality = (int)cb.GetSelectedData();
  689.         }
  690.         /// <summary>Called when the vertex processing changes</summary>
  691.         private void OnVertexProcessingChanged(object sender, EventArgs e)
  692.         {
  693.             ComboBox cb = sender as ComboBox;
  694.  
  695.             CreateFlags behavior = globalSettings.BehaviorFlags;
  696.  
  697.             // Clear flags
  698.             behavior &= ~CreateFlags.HardwareVertexProcessing;
  699.             behavior &= ~CreateFlags.SoftwareVertexProcessing;
  700.             behavior &= ~CreateFlags.PureDevice;
  701.             behavior &= ~CreateFlags.MixedVertexProcessing;
  702.  
  703.             // Determine new flags
  704.             CreateFlags newFlags = (CreateFlags)vertexCombo.GetSelectedData();
  705.             if ((newFlags & CreateFlags.PureDevice) != 0)
  706.                 newFlags |= CreateFlags.HardwareVertexProcessing;
  707.  
  708.             // Make changes
  709.             globalSettings.BehaviorFlags = behavior | newFlags;
  710.         }
  711.         /// <summary>Called when the presentation interval changes</summary>
  712.         private void OnPresentIntervalChanged(object sender, EventArgs e)
  713.         {
  714.             ComboBox cb = sender as ComboBox;
  715.             globalSettings.presentParams.PresentationInterval = (PresentInterval)cb.GetSelectedData();
  716.         }
  717.         /// <summary>Called when the clip to window state changes</summary>
  718.         private void OnClipWindowChanged(object sender, EventArgs e)
  719.         {
  720.             Checkbox cb = sender as Checkbox;
  721.             if (cb.IsChecked)
  722.                 globalSettings.presentParams.PresentFlag |= PresentFlag.DeviceClip;
  723.             else
  724.                 globalSettings.presentParams.PresentFlag &= ~PresentFlag.DeviceClip;
  725.         }
  726.         /// <summary>Called when the fullscreen or windowed item changes</summary>
  727.         private void OnWindowedFullscreenChanged(object sender, EventArgs e)
  728.         {
  729.             bool isWindowed = windowedButton.IsChecked;
  730.             globalSettings.presentParams.Windowed = isWindowed;
  731.  
  732.             // Set the control enabled or disabled properties
  733.             dialog.SetControlEnable((int)SettingsDialogControlIds.AdapterFormatLabel, !isWindowed);
  734.             dialog.SetControlEnable((int)SettingsDialogControlIds.ResolutionLabel, !isWindowed);
  735.             dialog.SetControlEnable((int)SettingsDialogControlIds.RefreshRateLabel, !isWindowed);
  736.  
  737.             dialog.SetControlEnable((int)SettingsDialogControlIds.AdapterFormat, !isWindowed);
  738.             dialog.SetControlEnable((int)SettingsDialogControlIds.Resolution, !isWindowed);
  739.             dialog.SetControlEnable((int)SettingsDialogControlIds.RefreshRate, !isWindowed);
  740.             dialog.SetControlEnable((int)SettingsDialogControlIds.DeviceClip, isWindowed);
  741.  
  742.             bool deviceClip = ((globalSettings.presentParams.PresentFlag & PresentFlag.DeviceClip) != 0);
  743.  
  744.             // If windowed, get the appropriate adapter format from Direct3D
  745.             if (globalSettings.presentParams.Windowed)
  746.             {
  747.                 DisplayMode mode = Manager.Adapters[(int)globalSettings.AdapterOrdinal].CurrentDisplayMode;
  748.                 globalSettings.AdapterFormat = mode.Format;
  749.                 globalSettings.presentParams.BackBufferWidth = mode.Width;
  750.                 globalSettings.presentParams.BackBufferHeight = mode.Height;
  751.                 globalSettings.presentParams.FullScreenRefreshRateInHz = mode.RefreshRate;
  752.             }
  753.  
  754.             // Update the clip check box
  755.             clipBox.IsChecked = deviceClip;
  756.  
  757.             // Update the adapter format list
  758.             adapterFormatCombo.Clear();
  759.  
  760.             EnumDeviceInformation edi = GetCurrentDeviceInfo();
  761.  
  762.             if (isWindowed)
  763.             {
  764.                 if (!adapterFormatCombo.ContainsItem(globalSettings.AdapterFormat.ToString()))
  765.                     adapterFormatCombo.AddItem(globalSettings.AdapterFormat.ToString(), globalSettings.AdapterFormat);
  766.             }
  767.             else
  768.             {
  769.                 // Add all the supported formats
  770.                 foreach(EnumDeviceSettingsCombo edsc in edi.deviceSettingsList)
  771.                 {
  772.                     if (!adapterFormatCombo.ContainsItem(edsc.AdapterFormat.ToString()))
  773.                         adapterFormatCombo.AddItem(edsc.AdapterFormat.ToString(), edsc.AdapterFormat);
  774.                 }
  775.             }
  776.             adapterFormatCombo.SetSelectedByData(globalSettings.AdapterFormat);
  777.             // Adapter format changed, update there
  778.             OnAdapterFormatChange(adapterFormatCombo, EventArgs.Empty);
  779.  
  780.             // Update resolution
  781.             if (isWindowed)
  782.             {
  783.                 resolution.Clear();
  784.                 AddResolution((short)globalSettings.presentParams.BackBufferWidth, (short)globalSettings.presentParams.BackBufferHeight);
  785.             }
  786.             resolution.SetSelectedByData(NativeMethods.MakeUInt32(
  787.                 (short)globalSettings.presentParams.BackBufferWidth, (short)globalSettings.presentParams.BackBufferHeight));
  788.  
  789.             // Resolution changed
  790.             OnResolutionChanged(resolution, EventArgs.Empty);
  791.  
  792.             // Update refresh
  793.             if (isWindowed)
  794.             {
  795.                 refreshCombo.Clear();
  796.                 AddRefreshRate(globalSettings.presentParams.FullScreenRefreshRateInHz);
  797.             }
  798.  
  799.             // Select the correct refresh rate
  800.             refreshCombo.SetSelectedByData(globalSettings.presentParams.FullScreenRefreshRateInHz);
  801.  
  802.             // refresh rate changed
  803.             OnRefreshRateChanged(refreshCombo, EventArgs.Empty);
  804.         }
  805.         /// <summary>Called when the cancel button is clicked</summary>
  806.         private void OnCancelClicked(object sender, EventArgs e)
  807.         {
  808.             // Nothing left to do, quit showing the screen
  809.             parent.ShowSettingsDialog(false);
  810.         }
  811.         /// <summary>Called when the ok button is clicked</summary>
  812.         private void OnOkClicked(object sender, EventArgs e)
  813.         {
  814.             // The device needs to be updated
  815.             if (globalSettings.presentParams.Windowed)
  816.             {
  817.                 globalSettings.presentParams.FullScreenRefreshRateInHz = 0;
  818.                 globalSettings.presentParams.BackBufferWidth = (int)windowWidth;
  819.                 globalSettings.presentParams.BackBufferHeight = (int)windowHeight;
  820.             }
  821.  
  822.             if (globalSettings.presentParams.MultiSample != MultiSampleType.None)
  823.             {
  824.                 globalSettings.presentParams.PresentFlag &= ~PresentFlag.LockableBackBuffer;
  825.             }
  826.  
  827.             // Create a device
  828.             parent.CreateDeviceFromSettings(globalSettings);
  829.  
  830.             // Stop showing the dialog now
  831.             parent.ShowSettingsDialog(false);
  832.         }
  833.         #endregion
  834.     }
  835. }